package com.wclass.controller.sys;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wclass.common.ReturnObject;
import com.wclass.entity.BaseAppUser;
import com.wclass.entity.BaseId;
import com.wclass.entity.BaseOrgan;
import com.wclass.entity.BaseTeamUser;
import com.wclass.interceptor.BaseContextHandler;
import com.wclass.interceptor.IgnoreAuth;
import com.wclass.service.IBaseAppUserService;
import com.wclass.service.IBaseIdService;
import com.wclass.service.IBaseOrganService;
import com.wclass.service.IBaseTeamUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户 后台控制器
 * </p>
 *
 * @author Bruce
 * @since 2018-12-20
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private IBaseAppUserService iBaseAppUserService;
    @Autowired
    private IBaseOrganService iBaseOrganService;
    @Autowired
    private IBaseTeamUserService iBaseTeamUserService;
    @Autowired
    private IBaseIdService iBaseIdService;

    /**
     * 获取个人老师审核列表（审核状态）
     *
     * @param limit
     * @param offset
     * @param auditStatus
     * @param username
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/getTeacherList", method = RequestMethod.GET)
    public ReturnObject<List<BaseAppUser>> getTeacherList(
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
            @ApiParam("审核状态") Integer auditStatus,
            @ApiParam("老师名字") String username) {

        try {
            EntityWrapper<BaseAppUser> ew = new EntityWrapper<>();
            if (auditStatus != null) {
                ew.eq("audit_status", auditStatus);
            }
            if (StringUtils.isNotBlank(username)) {
                ew.like("username", username);
            }
            ew.eq("type", 1);
            ew.orderBy("crt_time", false);
            Page<BaseAppUser> obj = iBaseAppUserService.selectPage(new Page<>(offset, limit), ew);
            return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject<>(12, "系统偷懒了");
        }
    }

    /**
     * 获取用户列表（通过审核）
     * @param limit
     * @param offset
     * @param username
     * @param type
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/getUserList", method = RequestMethod.GET)
    public ReturnObject<List<BaseAppUser>> getUserList(
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
            @ApiParam("名字") String username,
            @ApiParam("角色") Integer type) {

        try {
            EntityWrapper<BaseAppUser> ew = new EntityWrapper<>();
            ew.eq("audit_status", 0);
            if (StringUtils.isNotBlank(username)) {
                ew.like("username", username);
            }
            if(type!=null){
                ew.eq("type", type);
            }
            ew.orderBy("crt_time", false);
            Page<BaseAppUser> obj = iBaseAppUserService.selectPage(new Page<>(offset, limit), ew);
            return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject<>(12, "系统偷懒了");
        }
    }

    /**
     * 对个人进行审核
     *
     * @param auditStatus
     * @param auditOpinion
     * @param id
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/updateAuditStatus", method = RequestMethod.GET)
    public ReturnObject updateAuditStatus(@ApiParam("审核状态") @RequestParam Integer auditStatus,
                                          @ApiParam("审核意见") @RequestParam String auditOpinion,
                                          @ApiParam("个人id") @RequestParam Long id) {
        try {
            BaseAppUser baseAppUser = iBaseAppUserService.selectOne(new EntityWrapper<BaseAppUser>().eq("id", id));
            if (baseAppUser != null) {
                baseAppUser.setType(1);
                baseAppUser.setRoldId(1);
                baseAppUser.setAuditStatus(auditStatus);
                baseAppUser.setAuditOpinion(auditOpinion);
                baseAppUser.setAuditTime(new Date());

                /*
                 * 个人认证用户默认创建团队
                 */
                BaseId baseId = iBaseIdService.getNoUseId(3);

                BaseOrgan organ =  new BaseOrgan();
                organ.setId(Long.parseLong(baseId.getId() + ""));
                organ.setUserId(BaseContextHandler.getUserID());
                organ.setCrtTime(new Date());
                organ.setType(2);
                iBaseOrganService.insert(organ);

                baseId.setOrganState(1);
                iBaseIdService.updateById(baseId);

                //用户本人成为团队创建者
                baseAppUser.setRoldId(1);
                baseAppUser.setInstitutionId(organ.getId());
                baseAppUser.setInstitutionInviteState(2);
                iBaseAppUserService.updateById(baseAppUser);

                //团队成员列表添加记录
                BaseTeamUser teamUser = new BaseTeamUser();
                teamUser.setCrtTime(new Date());
                teamUser.setTeamId(baseAppUser.getInstitutionId());
                teamUser.setUserId(baseAppUser.getId());
                teamUser.setPosition(1);
                teamUser.setShowType(0);
                iBaseTeamUserService.insert(teamUser);

                return new ReturnObject(0, "审核成功");
            } else {
                return new ReturnObject(1, "审核失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject(12, "系统偷懒了");
        }
    }


    /**
     * 对个人进行禁用或者恢复
     *
     * @param disabled
     * @param id
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/updateDisabled", method = RequestMethod.GET)
    public ReturnObject updateDisabled(@ApiParam("禁用状态") @RequestParam Integer disabled,
                                       @ApiParam("个人id") @RequestParam Long id) {
        try {
            BaseAppUser baseAppUser = iBaseAppUserService.selectOne(new EntityWrapper<BaseAppUser>().eq("id",
                    id));
            if (baseAppUser != null) {
                int flag = iBaseAppUserService.updateDisabled(id, disabled);
                if (flag > 0) {
                    return new ReturnObject(0, "状态设置成功");
                }
            }
            return new ReturnObject(1, "状态设置失败");

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject(12, "系统偷懒了");
        }
    }


}

